package com.dwd.org.controller;


import com.dwd.basic.utils.AjaxResult;
import com.dwd.basic.utils.PageBean;
import com.dwd.org.pojo.DictionaryType;
import com.dwd.org.query.DictionaryTypeQuery;
import com.dwd.org.service.IDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/15 15:04
 * @Version 1.0
 */
@RestController
@RequestMapping("/dictType")
public class DictionaryTypeController {
    @Autowired
    private IDictionaryTypeService dictionaryTypeService;

    /**
     * 新增或者修改
     *
     * @param dictionaryType 如果id为空，则是新增，如果id不为空，则是修改
     * @return 返回成功还是失败
     */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody DictionaryType dictionaryType) {

        try {
            if (dictionaryType.getId() == null) {
                dictionaryTypeService.save(dictionaryType);
            } else {
                dictionaryTypeService.update(dictionaryType);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
        return AjaxResult.me();
    }


    /**
     * 批量删除
     *
     * @param dictionaryTypes
     * @return
     */
    @PostMapping("/batchRemove")
    public AjaxResult batchRemove(@RequestBody List<DictionaryType> dictionaryTypes) {
        try {
            //批量删除
            dictionaryTypeService.batchRemove(dictionaryTypes);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            dictionaryTypeService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage());
        }
        return AjaxResult.me();
    }

    @GetMapping("/{id}")
    public DictionaryType queryById(@PathVariable("id") Long id) {
        return dictionaryTypeService.queryById(id);
    }

    @GetMapping
    public List<DictionaryType> queryAll() {
        return dictionaryTypeService.queryAll();
    }

    @PostMapping
    public PageBean<DictionaryType> queryPage(@RequestBody DictionaryTypeQuery query) {
        return dictionaryTypeService.queryPage(query);
    }


}
